package pdi.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import utils.IConstants;

import au.com.bytecode.opencsv.CSVReader;

import com.google.common.base.Strings;

public class BoundingBox {

	private int label;
	private double left;
	private double top;
	private double width;
	private double height;
	private double score;


	public BoundingBox(int label, double left, double top, double width,
			double height) {
		this(label,left,top,width,height,0);
	}

	public BoundingBox(int label, double left, double top, double width,
			double height, double score) {
		super();
		this.label = label;
		this.left = left;
		this.top = top;
		this.width = width;
		this.height = height;
		this.score = score;
	}
	public int getLabel() {
		return label;
	}
	public void setLabel(int label) {
		this.label = label;
	}
	public double getLeft() {
		return left;
	}
	public void setLeft(double left) {
		this.left = left;
	}
	public double getTop() {
		return top;
	}
	public void setTop(double top) {
		this.top = top;
	}
	public double getWidth() {
		return width;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}

	public static List<BoundingBox> readFile(String annotationPath) throws IOException {
		CSVReader csv = null;			
		List<BoundingBox> bbs = new ArrayList<BoundingBox>();

		if(!Strings.isNullOrEmpty(annotationPath)){
			File f = new File(annotationPath);
			if(f.exists()){
				csv = new CSVReader(new FileReader(f), ' ','"', '\0');
				String[] fields = csv.readNext();
				while ( (fields = csv.readNext()) != null) {

					BoundingBox bb = new BoundingBox(getLabelValue(fields[0]), Integer.parseInt(fields[1]),
							Integer.parseInt(fields[2]), Integer.parseInt(fields[3]),
							Integer.parseInt(fields[4]), Double.parseDouble(fields[5]));

					bbs.add(bb);
				}
				
				csv.close();

			}

		}

		return bbs;
	}

	public static int getLabelValue(String name){
		int ret = IConstants.NON_PERSON;
		
		switch (name) {
		case "person":
			ret = IConstants.PERSON;
			break;
		case "people":
			ret = IConstants.PEOPLE;
			break;
		case "?person":
			ret = IConstants.AMB_PERSON;
			break;
		default:
			ret = IConstants.NON_PERSON;
			break;
		}
		
		return ret;
	}

}
